package com.example.activitydemo

import android.app.AlertDialog
import android.content.Intent
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity生命周期"
    private val textView:TextView by lazy(LazyThreadSafetyMode.NONE) { findViewById(R.id.tv_test) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d(TAG,"--onCreate--" + "---MainActivity创建了---")
        initView()
    }

    private fun initView() {
        textView.setOnClickListener {
            /*val intent = Intent(MainActivity@this,DialogActivity::class.java)
            startActivity(intent)*/
            //不带主题时生命周期
            showNoThemeDialog()
            //带主题生命周期
            //showThemeDialog()
        }
    }

    fun showNoThemeDialog(){
        val dialog: AlertDialog.Builder = AlertDialog.Builder(this@MainActivity)
        dialog.setIcon(R.mipmap.ic_launcher_round)
            .setCancelable(true)
            .setMessage("不带主题时Dialog的生命周期")
            .setTitle("这是一个不带主题的Dialog")
            .show()
    }

    fun showThemeDialog(){
        val dialog: AlertDialog.Builder = AlertDialog.Builder(this@MainActivity,R.style.AlertDialogStyle)
        dialog.setIcon(R.mipmap.ic_launcher_round)
            .setCancelable(true)
            .setMessage("带主题时Dialog的生命周期")
            .setTitle("这是一个带主题的Dialog")
            .show()
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG,"--onRestart-" + "---MainActivity重新创建了---")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG,"--onResume--" + "---MainActivity已经可见了---")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG,"--onStart--" + "---MainActivity已经启动了---")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG,"--onPause--" + "---MainActivity已经暂停了---")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG,"--onStop--" + "---MainActivity已经停止了---")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG,"--onDestroy--" + "---MainActivity已经销毁了---")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d(TAG,"--newConfig--" + "---MainActivity屏幕切换了---")
    }

}